//
//  KRHttpRecipeTool.m
//  cookBook
//
//  Created by kuroken on 16/1/28.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRHttpRecipeTool.h"
#import "KRHttpTool.h"

NSString * const KRIndex = @"Index.index";
NSString * const KRSearchGetList = @"Search.getList";
NSString * const KRSearchGetSuggestion = @"Search.getSuggestion";
NSString * const KRSearchGetCatelist = @"Search.getcatelist";
NSString * const KRRecipeetAlbumList = @"Recipe.getAlbumList";
NSString * const KRInfoGetAlbumInfo = @"Info.getAlbumInfo";
NSString * const KRInfoGetInfo = @"Info.getInfo";
NSString * const KRReadGetList = @"Read.getList";
NSString * const KRTopicView = @"Topic.view";
NSString * const KRInitWebView = @"WebView.initWebView";
NSString * const KRTopicIndex = @"Topic.index";

@implementation KRHttpRecipeTool

+ (NSURLSessionDataTask *)getParams:(void (^)(NSString ** _Nonnull, NSMutableDictionary *))params success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
    NSMutableDictionary *recipeParams = [NSMutableDictionary dictionary];
    recipeParams[@"appid"] = @"4"; // errormsg = 不存在的APP
    recipeParams[@"format"] = @"json"; // errormsg = 不支持的输出格式
    recipeParams[@"v"] = @"2"; // errormsg = 错误的APP key
    recipeParams[@"vc"] = @"40"; // errormsg = 调用类或方法不存在
    recipeParams[@"vn"] = @"v5.1.0"; // 版本号

    NSString *method;
    NSMutableDictionary *requestParams = [NSMutableDictionary dictionary];
    if (params) {
        params(&method, requestParams);
    }
    
    recipeParams[@"method"] = method; // errormsg = 调用类或方法不存在
    [recipeParams addEntriesFromDictionary:requestParams];
    
    NSString *url = @"http://api.hoto.cn/index.php";
    return [KRHttpTool get:url params:recipeParams success:^(NSURLSessionDataTask *task, id json) {
        if (success) {
            success(task, json);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(task, error);
        }
    }];
}

@end
